package cn.sumpu.app.guoguojie.dao.impl;


import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.guoguojie.dao.AppActivityDao;
import cn.sumpu.app.guoguojie.domain.AppActivity;
import cn.sumpu.app.guoguojie.domain.Ext.AppActivityExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class AppActivityDaoImpl extends SqlSessionDaoSupport implements AppActivityDao{

	@Override
	public void insert(AppActivity appActivity) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.insert",
					appActivity);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@Override
	public void update(AppActivity appActivity) throws AppBusinessException {
		try {
			getSqlSession().update(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.update",
					appActivity);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update appActivity failure.", e);
			}
			throw new AppBusinessException("update appActivity failure.", e);
		}
		
	}

	@Override
	public AppActivityExt getAppActivityById(String identifier)
			throws AppBusinessException {
		try {
			return (AppActivityExt) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getAppActivityById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query failure.", e);
			}
			throw new AppBusinessException("query failure.", e);
		}
	}

	@Override
	public void deleteById(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.AppActivityMapper.deleteById",
							identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete failure.", e);
			}
			throw new AppBusinessException("delete failure.", e);
		}

		
	}
	@SuppressWarnings("unchecked")
	public List<AppActivityExt> getAppActivity(Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<AppActivityExt>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getAppActivity",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getAppActivityCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getAppActivityCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAppActivityCount failure.", e);
			}
			throw new AppBusinessException(
					"getAppActivityCount failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	public List<AppActivityExt> getMyFamilyAppActivity(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<AppActivityExt>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getMyFamilyAppActivity",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getMyFamilyAppActivityCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getMyFamilyAppActivityCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMyFamilyAppActivityCount failure.", e);
			}
			throw new AppBusinessException(
					"getMyFamilyAppActivityCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<AppActivityExt> getMyFamilyJoinAppActivity(
			Map<String, Object> map) throws AppBusinessException {
		try {
			return (List<AppActivityExt>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getMyFamilyJoinAppActivity",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getMyFamilyJoinAppActivityCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getMyFamilyJoinAppActivityCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMyFamilyJoinAppActivityCount failure.", e);
			}
			throw new AppBusinessException(
					"getMyFamilyJoinAppActivityCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<AppActivityExt> getRegisteredList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<AppActivityExt>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getRegisteredList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public int getRegisteredListCount(String activityId)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.AppActivityMapper.getRegisteredListCount",
							activityId);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getRegisteredListCount failure.", e);
			}
			throw new AppBusinessException(
					"getRegisteredListCount failure.", e);
		}
	}

}
